const db = require('../config/db');

exports.addGood = async (req, res) => {
    const { name, price, stock, description, image, code, category, brand } = req.body;
    const [result] = await db.query('INSERT INTO goods (name, price, stock, description, image, code, category_id, brand_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?)', [name, price, stock, description, image, code, category, brand]);
    res.json({ message: '商品添加成功', id: result.insertId,code:200 });
}

exports.getGoodsList = async (req, res) => {
    try {
        const page = parseInt(req.query.page) || 0;
        const limit = parseInt(req.query.limit) || 10;
        
        const [result] = await db.query('SELECT * FROM goods LIMIT ?, ?', [page, limit]);
        res.status(200).json({ message: '商品列表获取成功', data: result });
    } catch (error) {
        console.error('Error fetching goods list:', error);
        res.status(500).json({ message: '获取商品列表失败', error: error.message });
    }
}

